/**
 * Created by ZFL on 2017/9/17.
 */
var  selectType=$.getPara('selectType');
var vm=new Vue({
    el:'#rank',
    data:{
        rankInfo:[],     //排名信息(显示在销售排行,佣金排行,战队排名的页面)
        myInfo:{},    //本人信息(显示在我的招募的页面)
        perInfo:[],
        corpsName:'',
        rank:0,          //本战队的排名
        salesRankInfo:[],    //销售排行信息
        commissionRankInfo:[],   //佣金排行信息
        squadronRankInfo:[],      //战队排行信息
        shopKeepersInfo:[],        //我的招募信息   招募的店主
        usersInfo:[],             //我的招募信息    招募的普通用户
        playersInfo:[],         //队员信息
        userImage:'',
        id:0,
        userId:0

    },

})
vm.userId= $.getPara('userId');
function appInit(){
  PageInit();
}
Vue.filter('price',function(value){
    return divideNumber(value,100);
})


function PageInit() {
    infiniteSroll();

    if(selectType==1){
        var loading = false;
        getRanks()
    }else if(selectType==2){
        var loading = false;
        getRanks()
    }else if(selectType==3){
        var loading = false;
        squadronInfo();    //
        getRanks()
    }else if(selectType==4){
        var loading = false;
        getUserInfo()
        getRanks();
    }else if(selectType==5){
        var loading = false;
        getRanks()
    }else if(selectType==6){
        var loading = false;
        getUserInfo()
        getRanks()
    }


}
var pageNum = 1;
var pageSize = 10;
var userId=$.getPara('userId');
//得到用户的信息
function getUserInfo(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userId=userId;
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api:'yg_baijiaapp/user/getUserEntityByIdNoPassword',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                vm.userImage=data.picture;
            }
        }

    })
}
//我的战队信息
function squadronInfo(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.userId=userId;
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api:'yg_baijiaapp/statistics/getHuiMeiTeamInfo',
        success:function(result){
            if(result.code==10000){
                var data=result.data;

                vm.corpsName=data.corpsName;
                vm.rank=data.rank;

            }
        }
    })
}


/******排行******/
function getRanks(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.selectType=selectType;
            obj.userId=userId;
            obj.pageNum = pageNum;
            obj.pageSize = pageSize;
            if($.getPara('special')==2) {
            }else if(!$.getPara('special')){
                obj.special=1;
            }
            console.log(obj);
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        visible:'hide',
        api:'yg_baijiaapp/statistics/getSalesCommissionRank',
        success:function(result){
            if(result.code===10000) {
                var data=result.data;
                if(data.length > 0){
                    // 加载数据
                    loading = false;

                    //console.log(vm.commentList);
                    if (selectType == 1) {
                        var data1 = result.data
                        data1.forEach(function(val,ind){
                            vm.salesRankInfo.push(val);
                        })
                        var unique1 = {};
                        vm.usersInfo.forEach(function(gpa){ unique1[ JSON.stringify(gpa) ] = gpa });
                        vm.usersInfo = Object.keys(unique1).map(function(u){return JSON.parse(u) });
                    } else if (selectType == 2) {
                        var data2 = result.data;
                        data2.forEach(function(val,ind){
                            vm.commissionRankInfo.push(val);
                        })
                        var unique2 = {};
                        vm.usersInfo.forEach(function(gpa){ unique2[ JSON.stringify(gpa) ] = gpa });
                        vm.usersInfo = Object.keys(unique2).map(function(u){return JSON.parse(u) });
                    } else if (selectType == 3) {
                        var data3 = result.data;
                        data3.forEach(function(val,ind){
                            vm.squadronRankInfo.push(val);
                        })
                    } else if (selectType == 4) {
                        var data4 = result.data;
                        data4.forEach(function(val,ind){
                            vm.shopKeepersInfo .push(val);
                        })
                    } else if (selectType == 5) {
                        var data5 = result.data;
                        data5.forEach(function(val,ind){
                            vm.playersInfo.push(val);
                        })
                        var unique5 = {};
                        vm.usersInfo.forEach(function(gpa){ unique5[ JSON.stringify(gpa) ] = gpa });
                        vm.usersInfo = Object.keys(unique5).map(function(u){return JSON.parse(u) });
                    } else if (selectType == 6){
                        var data6 = result.data;
                        data6.forEach(function(val,ind){
                            vm.usersInfo .push(val);
                        })
                    }else{

                    }
                    //表示这是最后一页数据
                    //if(data.length == pageNum){
                    //    loading = true;
                    //}
                }else {
                    //YDUI.dialog.toast('无更多数据',1000);
                    $(document.body).destroyInfinite();
                }

            }else{
                    YDUI.dialog.toast(result.message, 'error', 1000);
                }
            }

    })
}
//滚动加载信息
function infiniteSroll() {
    $("body").infinite().on("infinite", function () {
        if (loading) return;
        loading = true;
        pageNum++;

        $('.weui-infinite-scroll-box').show();
        setTimeout(function () {
            loading = false;
            $('.weui-infinite-scroll-box').hide();
            getRanks();
        }, 1500);   //模拟延迟
    });
}